package cup.cqzj.auth.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import cup.common.common.MyRoleConstant;
import cup.core.cache.utils.CacheUtil;
import cup.core.launch.constant.AppConstant;
import cup.core.log.annotation.ApiLog;
import cup.core.redis.cache.CupRedis;
import cup.core.secure.annotation.PreAuth;
import cup.core.tenant.annotation.NonDS;
import cup.core.tool.api.R;
import cup.core.tool.support.Kv;
import cup.core.tool.utils.Func;
import cup.cqzj.auth.dto.DsfUserDTO;
import cup.cqzj.auth.service.DsfTokenGranter;
import cup.cqzj.auth.vo.CheckCodeVo;
import cup.modules.auth.utils.TokenUtil;
import cup.modules.system.entity.Role;
import cup.modules.system.entity.User;
import cup.modules.system.entity.UserInfo;
import cup.modules.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

import static cup.core.cache.constant.CacheConstant.USER_CACHE;

/**
 * @author : ruoyu
 * @date : 2022/8/9
 */
@NonDS
@ApiSort(1)
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_AUTH_NAME + "/act")
@Api(value = "激活用户", tags = "激活用户")
@Slf4j
public class CqjgptActEndPoint {

	private final DsfTokenGranter dsfTokenGranter;
	private final CupRedis cupRedis;

	@ApiLog("激活第三方绑定账户")
	@PostMapping("/act")
	@ApiOperation(value = "激活第三方绑定账户(市级/admin 可调用)", notes = "激活第三方绑定账户")
	@ApiOperationSupport(order = 1)
	@PreAuth(MyRoleConstant.HAS_ROLE_CITY_ADMIN_APPROVAL)
	public R activateUser(Long userId) {
		dsfTokenGranter.activateUser(userId);
		return R.status(true);
	}

	@ApiLog("简单-绑定一网通账户")
	@PostMapping("/easy-bond-add")
	@ApiOperation(value = "绑定用户", notes = "绑定用户")
	@ApiOperationSupport(order = 2)
	@PreAuth(MyRoleConstant.HAS_ROLE_CITY_ADMIN_APPROVAL)
	public R easyBondDdd(@ApiParam(name = "用户id", required = true) Long userId,
						 @ApiParam(name = "第三方loginId", required = true) String loginId) {
		cupRedis.del(CacheUtil.formatCacheName(USER_CACHE, true)+"*");
		dsfTokenGranter.easyBondDdd(userId, loginId);
		return R.success("绑定成功!");
	}

	@ApiLog("简单-解绑第三方用户")
	@PostMapping("/easy-unbind-add")
	@ApiOperation(value = "解绑用户", notes = "解绑用户")
	@ApiOperationSupport(order = 3)
	@PreAuth(MyRoleConstant.HAS_ROLE_CITY_ADMIN_APPROVAL)
	public R easyUnbindAdd(@ApiParam(name = "用户id", required = true) Long userId) {
		cupRedis.del(CacheUtil.formatCacheName(USER_CACHE, true)+"*");
		dsfTokenGranter.easyUnbindAdd(userId);
		return R.success("解绑成功");
	}
}
